﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Service;
using MODEL;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using Microsoft.Practices.ServiceLocation;
using GalaSoft.MvvmLight.Views;
using Common;
using System.ComponentModel;

namespace ViewModel.StationManager
{
    public class StationUserMapSettingViewModel:ViewModelBase
    {
        public ICommand SaveStationMapCommand { get; set; }

        private User m_User;
        public User User
        {
            get { return this.m_User; }
            set { this.m_User = value; base.RaisePropertyChanged(() => this.User); }
        }

        private List<StationInfo> m_StationInfoList = new List<StationInfo>();
        public List<StationInfo> StationInfoList
        {
            get { return this.m_StationInfoList; }
            set { this.m_StationInfoList = value; base.RaisePropertyChanged(() => StationInfoList); }
        }

        private IStationServcie m_StationService;

        public StationUserMapSettingViewModel(IStationServcie stationService)
        {
            this.m_StationService = stationService;
            this.SaveStationMapCommand = new RelayCommand(SaveStationMap);
            this.PropertyChanged += OnPropertyChange;           
        }

        public async void OnPropertyChange(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "User")
            {
                try
                {
                    if (this.User != null && !string.IsNullOrEmpty(this.User.UserId))
                    {
                        List<StationInfo> allStationInfo = (await this.m_StationService.QueryStationsAsync()).ToList();

                        List<StationInfo> userStationInfo = (await this.m_StationService.QueryStationsAsync(new StationQueryCriteria { UserId = this.User.UserId })).ToList();

                        allStationInfo.ForEach(station => station.IsChecked = userStationInfo.Contains(station));

                        this.StationInfoList = allStationInfo;
                    }
                }
                catch (Exception ex)
                {
                    await ServiceLocator.Current.GetInstance<IDialogService>().ShowError("站点信息加载失败 ", "错误", "确定", null);
                    Log.WriteException(ex);
                }
            }
        }

        public async void SaveStationMap()
        {
            List<StationInfo> checkedStations = this.StationInfoList.Where(s => s.IsChecked).ToList();
            if (!checkedStations.SafeCheck())
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowMessage("未选中任何数据", "提示");
                return;
            }
            try
            {
                Task<int> task = this.m_StationService.MapUserWithStationAsync(this.User.UserId, checkedStations);
                task.Wait();
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowMessage("保存成功", "成功");
                this.MessengerInstance.Send<object>(null, "StationUserMapSetting_CloseForm");
            }
            catch (Exception ex)
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowError("保存失败", "错误", "确定", null);
                Log.WriteException(ex);
            }
        }
    }
}
